package com.classic.duanzhi;

import android.content.Intent;
import android.os.Handler;
import android.support.design.widget.TabLayout;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;

import com.classic.adapter.ListAdapter;
import com.classic.base.BaseActivity;
import com.classic.dialog.SelectLabelDialog;
import com.qy.sdk.Interfaces.RDInterface;
import com.qy.sdk.Utils.NativeModel;
import com.qy.sdk.rds.NativeView;

import java.util.Arrays;
import java.util.List;

/**
 * 段子集中地
 */
public class EpisodeListActivity extends BaseActivity implements SelectLabelDialog.OnItemClickListener {

    @Override
    protected int getLayoutId() {
        return R.layout.ad_activity_episode_list;
    }

    public static final String data_ = "DATA_";

    public static void starThis(FragmentActivity activity, String title) {
        Intent intent = new Intent(activity, EpisodeListActivity.class);
        intent.putExtra(data_, title);
        activity.startActivity(intent);
    }

    public static void starThis(FragmentActivity activity) {
        activity.startActivity(new Intent(activity, EpisodeListActivity.class));
    }

    ViewPager viewpager;

    @Override
    protected void initView() {
        viewpager = findViewById(R.id.viewpager);
        final TabLayout tablayout = findViewById(R.id.tablayout);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                final String[] strings = getResources().getStringArray(R.array.table);
                ListAdapter listAdapter = new ListAdapter(getSupportFragmentManager(), Arrays.asList(strings));
                viewpager.setAdapter(listAdapter);
                tablayout.setupWithViewPager(viewpager);
                findViewById(R.id.iv_more).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        initSelectDialog(Arrays.asList(strings));
                    }
                });
            }
        }, 10);

        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                index = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
//
    }

    private int index = 0;

    private SelectLabelDialog dialog;

    void initSelectDialog(List<String> strings) {
        if (null == dialog) dialog = new SelectLabelDialog(this, strings, this);
        dialog.show(index);
    }

    @Override
    protected void initData() {

    }


    @Override
    public String getActivityTitle() {
        String s = getIntent().getStringExtra(data_);
        return TextUtils.isEmpty(s) ? getResources().getString(R.string.app_name) : s;
    }

    @Override
    public void onClick(int index) {
        viewpager.setCurrentItem(index);
    }
}
